/****************************************************/
/* Project Name:预约信息管理                             * / 
/* File Name:TdcReservationService              */ 
/* Date:2017年3月20日下午5:59:12            */ 
/* Description:预约信息管理的service的方法层       */
 /****************************************************/ 
package com.thinkgem.jeesite.modules.tdc.reservation.service;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.zxing.BarcodeFormat;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.QRCode;
import com.thinkgem.jeesite.modules.tdc.department.entity.TdcDepartment;
import com.thinkgem.jeesite.modules.tdc.reservation.dao.TdcReservationDao;
import com.thinkgem.jeesite.modules.tdc.reservation.entity.TdcReservation;
import com.thinkgem.jeesite.modules.tdc.room.entity.TdcRoom;
import com.thinkgem.jeesite.modules.tdc.times.entity.TdcTimes;

/**
 * 预约信息Service
 * @author 孟朋朋
 * @version 2017-03-20
 */
@Service
@Transactional(readOnly = true)
public class TdcReservationService extends CrudService<TdcReservationDao, TdcReservation> {
	
	@Autowired
	private TdcReservationDao tdcReservationDao ;
	public TdcReservation get(String id) {
		return super.get(id);
	}
	
	public List<TdcReservation> findList(TdcReservation tdcReservation) {
		return super.findList(tdcReservation);
	}
	
	public Page<TdcReservation> findPage(Page<TdcReservation> page, TdcReservation tdcReservation) {
		return super.findPage(page, tdcReservation);
	}
	
	
	@Transactional(readOnly = false)
	public void save(TdcReservation tdcReservation) {
		super.save(tdcReservation);
	}
	
	@Transactional(readOnly = false)
	public void delete(TdcReservation tdcReservation) {
		super.delete(tdcReservation);
	}
	
	

	/**
	 * 
	 * checkDate:(检查日期)
	 * 
	 * @param bespeakStart
	 * @param bespeakEnd
	 * @return 返回是否合法
	 */
	
	public boolean checkDate(Date bespeakStart,Date bespeakEnd,String start,String end,String delay){
		
		Calendar cal=Calendar.getInstance();
		cal.setTime(bespeakStart);
		int starMins = (cal.get(Calendar.HOUR_OF_DAY))*60 + cal.get(Calendar.MINUTE); 
		cal.setTime(bespeakEnd);
		int endMins = (cal.get(Calendar.HOUR_OF_DAY))*60 + cal.get(Calendar.MINUTE); 
		String[]stmp = start.split(":");
		int startmp = Integer.parseInt(stmp[0])*60 + Integer.parseInt(stmp[1]);
		stmp = end.split(":");
		int endtmp = Integer.parseInt(stmp[0])*60 + Integer.parseInt(stmp[1]) + Integer.parseInt(delay);
		if(((endMins-starMins)>5)&&(starMins>=startmp)&&(starMins<=endtmp)&&(endMins>=startmp)&&(endMins<=endtmp))
		
		{
		   return false;
		}
		
		return true;
		
	}
	
	/**
	 * 生成二维码图片,并保存在src/main/webapp/static/inages2code文件夹里,
	 * 返回该二维码图片的路径
	 * @param path 该二维码存的路径 ，以"/"结束
	 * @return 该二维码图片的名字
	 */
	public String get2Code(String path,String bsnumber){
		QRCode qrc = new QRCode();
		String filePostfix="png";
     	/*没有父目录，创建*/
		File parentfile = new File(path);
		if(!parentfile.exists()){
			parentfile.mkdirs();
		}
		File file = new File(path + "/" + bsnumber + "."+filePostfix);
		if(!file.exists())file.mkdirs();
	   /*生成QRCode二维码*/
		qrc.encode(bsnumber, file,filePostfix, BarcodeFormat.QR_CODE, 5000, 5000, null);

		return bsnumber + ".png";
	
	}
	
	
	/**
	 * 
	 * findNo:人员编号的接口方法,获取自动生成的编号
	 * @return {@link String}<{@link time}>
	 */
	public String getBNumber() {
        /*	获取当前系统时间的年月日*/
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String yyyymmdd = format.format(Calendar.getInstance().getTime());
		/*获取当前数据库中预约信息表里的最大的预约编号*/
		String bn01 = tdcReservationDao.getMaxBespeakNumber();
        /**取时间*/
		if(null == bn01 || "".equals(bn01)){
			return yyyymmdd+"001";
		}
		String bnymd = bn01.substring(0,8);
        /*取编号（001~999）*/
		String bnnum = bn01.substring(8,bn01.length());
       /*要获取的编号*/
		String bnumber;
	if(!yyyymmdd.equals(bnymd) || null == bnymd || "".equals(bnymd)){/*说明已经过了一天，或者数据库中没有值*/
			bnumber = yyyymmdd + "001";
			
		}else{
			int number = Integer.parseInt(bnnum) + 1;
			if(number<10){
				bnnum="00"+number;
			}else if(number>=10 && number<100){
				bnnum="0"+number;
			}else if(number>=100 && number<1000){
				bnnum=""+number;
			}else bnnum = "";/*超过999，获取的预约编号不对，后缀为"",后面根据获取的预约编号的长度（==11，正确，==8，不对）判断该预约编号是否正确。*/
			bnumber = yyyymmdd+bnnum;
		}
		return bnumber;
	}
	/**
	 * 
	 * bespeakDeptGet:(得到所有部门名)
	 * 
	 * @return 返回部门集合
	 */
	public List<TdcDepartment> findAllDeptList(){
		
		return tdcReservationDao.findAllDeptList();
	}	
	/**
	 * 
	 * todayCount:(当天预约总量)
	 * 
	 * @return 返回当天预约总量
	 */
	
	public int todayCount(){
		return tdcReservationDao.todayCount();
	}
	/**
	 * 
	 * arrivedCount:(当天已到数量)
	 * 
	 * @return 返回当天已到数量
	 */
	
	public int arrivedCount(){
		
		return tdcReservationDao.arrivedCount();
	}
	/**
	 * 
	 * periodList:(时间段查询)
	 * 
	 * @return 返回查询集合
	 */
	
	public List<TdcReservation> periodList(){
		return tdcReservationDao.periodList();
	}
	
	
	/**
	 * 
	 * getByBespeakNumber:(根据 预约编号查询相关的预约信息)
	 * 
	 * @param bespeakNumber 预约编号
	 * @return 返回预约类的信息
	 */
	public TdcReservation getByBespeakNumber(String bespeakNumber){
		
		TdcReservation tmp = tdcReservationDao.getByBespeakNumber(bespeakNumber); 
	
		return tmp;
	}
	/**
	 * 
	 * findRoomList:(找到所有合适的房间)
	 * 
	 * @param roomType:房间类型
	 * @param roomStatus：房间状态
	 * @return 返回合适房间集合
	 */
	public List<TdcRoom> findRoomList(String roomType,String roomStatus){
		
		return tdcReservationDao.findRoomList(roomType,roomStatus);
	}
	/**
	 * 
	 * updateStatus:(更新预约状态)
	 * 
	 * @param bespeakRoom:预约房间号
	 * @param bespeakStatus：预约状态
	 * @param bespeakNumber：预约编号
	 * @return 返回更新是否成功
	 */
	@Transactional(readOnly = false)
	public boolean  updateStatus(String bespeakRoom, String bespeakStatus,String bespeakNumber){
		
		return tdcReservationDao.updateStatus(bespeakRoom, bespeakStatus, bespeakNumber);
	}
	/**
	 * 
	 * updateRoomStatus:(更新房间状态)
	 * 
	 * @param roomStatus：房间状态
	 * @param roomNumber：房间编号
	 * @return 返回更新是否成功
	 */
	@Transactional(readOnly = false)
	public boolean updateRoomStatus( String roomStatus, String roomNumber){
		return tdcReservationDao.updateRoomStatus(roomStatus, roomNumber);
	}
	
	/**
	 * 
	 * getAmOrPm:(判断一天的上下午)
	 * 
	 * @param date ：日期
	 * @return 返回"上午"或"下午"
	 */
	public String getAmOrPm(Date date){
		        String s=null;
				int vHour = date.getHours(); 
				if (vHour < 12) 
				{ 
					s="上午";
				} 
				else 
				{ 
				    s="下午";
			
				} 
				return s;
	}
	/**
	 * 
	 * getWeek:(获取星期)
	 * 
	 * @param date：日期
	 * @return 返回星期
	 */
	public  String getWeek(Date date){ 
		/*规定格式*/
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		String week = sdf.format(date);
		return week;
	}
	/**
	 * 
	 * findTdcTimesList:(根据星期找到时间配置)
	 * 
	 * @param theWeek：星期
	 * @return 返回TdcTimes
	 */
	@Transactional(readOnly = false)
	public TdcTimes findTdcTimes(String theWeek){
		return tdcReservationDao.findTdcTimes(theWeek);
	}
	/**
	 * 
	 * findCountAm:(获取当天上下午预约数量)
	 * 
	 * @param bespeakStart
	 * @param bespeakEnd
	 * @return 返回当天上下午预约数量
	 */
	public int findCountAm(String  start,String end){
		
		
		return tdcReservationDao.findCountAm(start, end);
	}
	
}